/**
 * @file models/closedPositions.js
 * 平仓列表models
 * @author
 */

import { routerRedux } from 'dva/router';
import api from '../api';
import { delay } from '../utils/sagaEffects';

export default {
	namespace: 'closedPositions',
	state: {
		list: [],
		page: {},
		selected: undefined,
		isDetailOpen: false,
		closeOrder: undefined,
	},
	reducers: {
		getListSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				list: response.data,
				page: response.page,
				selected: undefined,
				isDetailOpen: false,
			};
		},
		changeSelected(state, action) {
			const { payload: { selected } } = action;
			return {
				...state,
				isDetailOpen: (selected !== undefined),
				selected,
			};
		},
		changeDetailOpen(state, action) {
			const { payload: { open } } = action;
			return {
				...state,
				isDetailOpen: open,
			};
		},
		getCloseOrderSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				closeOrder: response.data,
			};
		},
	},
	effects: {
		* getClosedPositions({ payload: query }, { call, put }) {
			const response = yield call(api.getClosedPositions, query);
			yield put({
				type: 'getListSuccess',
				payload: { response },
			});
		},
		* getCloseOrder({ payload: query }, { call, put }) {
			const response = yield call(api.getCloseOrder, query);
			yield put({
				type: 'getCloseOrderSuccess',
				payload: { response },
			});
		},
	},
	subscriptions: {},
};
